/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entities.User;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author HP
 */
@ManagedBean(name = "newuserbean")
@RequestScoped
public class NewUserBean {

    @PersistenceContext 
    private EntityManager em;
    @Resource 
    private UserTransaction utx;
    
    private String passwordv;
    private User user = new User();

    public User getUser() {
        return user;
    }
    
    public void setUser(User user) {
        this.user = user;
    }
    
    public String getPasswordv() {
        return passwordv;
    }

    public void setPasswordv(String passwordv) {
        this.passwordv = passwordv;
    }

    public void createUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        User foundUser = searchForUser();
        if (foundUser == null) {
            if (!user.getPassword().equals(passwordv)) {
                FacesMessage message = new FacesMessage("The specified passwords do not match.  Please try again");
                context.addMessage(user.getPassword(), message);

            } else {
                if(user.getPic()==null || user.getPic().trim().equals(""))
                        user.setPic("defaultUser.png");
                try {
                    
                    utx.begin();
                    em.persist(user);
                    utx.commit();

                } catch (Exception e) {
                    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "Error creating user!",
                            "Unexpected error when creating your account.  Please contact the system Administrator");
                    context.addMessage(null, message);
                    Logger.getAnonymousLogger().log(Level.SEVERE,
                            "Unable to create new user",
                            e);

                }
            }

        } else {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Username '"
                    + foundUser.getUsername()
                    + "' already exists!  ",
                    "Please choose a different username.");
            context.addMessage(foundUser.getUsername(), message);

        }
    }

    private User searchForUser() {
        try {
            return (User) em.createNamedQuery("User.findByUsername").setParameter("username", user.getUsername()).getSingleResult();
        } catch (NoResultException nre) {
            return null;
        }
    }
}
